package com.zzyl.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse selectByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page page = (Page) nursingProjectMapper.selectByPage(name,status);
        PageResponse of = PageResponse.of(page, NursingProjectVo.class);
        return of;
    }

    @Override
    public void addNursingProject(NursingProjectDto npd) {
        try {
            NursingProject nursingProject = BeanUtil.toBean(npd, NursingProject.class);
            nursingProject.setIsDelete(0);
            nursingProjectMapper.insertNursingProject(nursingProject);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_INSERT_FAIL);
        }
    }

    @Override
    public NursingProjectVo selectByID(Long id) {
        NursingProjectVo nursingProjectVo = nursingProjectMapper.selectByID(id);
        return nursingProjectVo;
    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        try {
            NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
            nursingProjectMapper.updateNursingProject(nursingProject);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_UPDATE_FAIL);
        }
    }

    @Override
    public void deleteNursingProject(Long id) {
        try {
            nursingProjectMapper.deleteNursingProject(id);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE_FAIL);
        }
    }

    @Override
    public void forbiddenNursingProject(Long id, Integer status) {
        try {
            nursingProjectMapper.updateForbiddenById(id,status);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_UPDATE_FAIL);
        }
    }

    @Override
    public List<NursingProjectVo> selectAll() {
        List<NursingProjectVo> list = nursingProjectMapper.selectByPage("",null);
        return list;
    }
}
